package pattern22;

import java.util.Hashtable;

/**
 * 是一个享元工厂，创建并管理享元对象
 * 用来确保合理地共享Flyweight，当用户请求一个Flyweight时，FlyweightFactory对象提供一个已经创建的实例或者创建一个
 * @author wdl
 */
public class FlyweightFactory {
    private Hashtable<String,IFlyweight> flyweights=new Hashtable<>();

    /**
     * 初始化工厂时生成三个实例
     */
    public FlyweightFactory(){
        flyweights.put("X",new ConcreteFlyweight());
        flyweights.put("Y",new ConcreteFlyweight());
        flyweights.put("Z",new ConcreteFlyweight());
    }

    public IFlyweight getFlyweight(String key){
        return flyweights.get(key);
    }
}
